/**
 * Created by IntelliJ IDEA.
 * User: feng_lh
 * Date: 12-4-1
 * Time: 下午2:33
 * To change this template use File | Settings | File Templates.
 */
//初始化  设置密码强度控件  start
$(document).ready(function() {
    //初始化  设置密码强度控件  end
    $("#new_password").change( function() {
        var pwd = $("#new_password").val();
        // 这里可以写些验证代码
        var Mcolor = "#EFEFEF",Lcolor = "#EFEFEF",Hcolor = "#EFEFEF";
        var m = 0;

        var Modes = 0;
        for (i = 0; i < pwd.length; i++)
        {
            var charType = 0;
            var t = pwd.charCodeAt(i);
            if (t >= 48 && t <= 57)
            {
                charType = 1;
            }
            else if (t >= 65 && t <= 90)
            {
                charType = 2;
            }
            else if (t >= 97 && t <= 122)
                charType = 4;
            else
                charType = 4;
            Modes |= charType;
        }

        for (i = 0; i < 4; i++)
        {
            if (Modes & 1) m++;
            Modes >>>= 1;
        }

        if (pwd.length <= 4)
        {
            m = 1;
        }

        switch (m)
        {
            case 1:
                Lcolor = "#E77717";
                Mcolor = Hcolor = "#EFEFEF";
                break;
            case 2:
                Mcolor = "#E77717";
                Lcolor = Hcolor = "#EFEFEF";
                break;
            case 3:
                Hcolor = "#E77717";
                Lcolor = Mcolor = "#EFEFEF";
                break;
            case 4:
                Hcolor = "#E77717";
                Lcolor = Mcolor = "#EFEFEF";
                break;
            default:
                Hcolor = Mcolor = Lcolor = "#EFEFEF";
                break;
        }
        $("#pwd_lower").css({"background-color":Lcolor});
        $("#pwd_middle").css({"background-color":Mcolor});
        $("#pwd_high").css({"background-color":Hcolor});
    });

});


var result = false; //设置结果 默认false
//修改密码 start
function modPsw() {
    var password = $("#password").val();
    var new_password = $("#new_password").val();
    var confirm_Password = $("#confirm_Password").val();
    if (!checkPassword()) {
        return false;
    }
    if (!checkNewPassword()) {
        return false;
    }
    if (!checkConfirmPassword()) {
        return false;
    }

    $.ajax({
                type:"POST",
                url:Top_Path.webRoot+"/index.php?g=product&m=member&a=save_pwd",
                data:{userPsw:new_password},
                dataType: "json",
                success:function(data) {
                    if (data.success == true) {
                        showDialog(" 修改密码成功! ");
                        location.reload();
                    }
                },
                error:function(XMLHttpRequest, textStatus) {
                    if (XMLHttpRequest.status == 500) {
                        var result = eval("(" + XMLHttpRequest.responseText + ")");
                        showDialog(result.jsonError.errorText);
                    }
                }
            });
}
//修改密码 end

//验证原始密码是否与该用户匹配 start
function checkPassword() {
    var password = $("#password").val();
    if (password == "") {
        fieldValidate("#passwordMsg", "#password_div", "#passwordTips", "请输入当前密码!");
        return false;
    }
    $.ajax({
                type:"POST",
                url:Top_Path.webRoot+"/index.php?g=product&m=member&a=get_old_pwd",
                data:{userPsw:password},
                dataType: "json",
                success:function(data) {
                    if (data.success == true) {
                        fieldValidatePass("#passwordMsg", "#password_div", "#passwordTips", "");
                        result = true;
                    }else{
                        fieldValidate("#passwordMsg", "#password_div", "#passwordTips", "密码错误!");
                        result = false;
                    }
                },
                error:function(XMLHttpRequest, textStatus) {
                    if (XMLHttpRequest.status == 500) {
                        var result = eval("(" + XMLHttpRequest.responseText + ")");
                        showDialog(result.jsonError.errorText);
                        result = false;
                    }
                }
            });
    return result;
}
//验证原始密码是否与该用户匹配 end

//验证新密码格式 start
function checkNewPassword() {
    var new_password = $("#new_password");
    var newStr = new_password.val();
    if (newStr == "") {
        fieldValidate("#new_passwordMsg", "#new_password_div", "#new_passwordTips", "新密码不能为空!");
        return false;
    }
    var new_passwordImg = $("#new_passwordImg");
    var new_passwordMsg = $("#new_passwordMsg");
    if (newStr.length >= 6 && newStr.length <= 16) {
        var confirmStr = $("#confirm_Password").val();
        if (confirmStr != "") {
            if (newStr != confirmStr) {
                fieldValidate("#confirm_PasswordMsg", "#confirm_Password_div", "#confirm_PasswordTips", "确认密码与新密码不一致!");
                return false;
            }
        }
        fieldValidatePass("#new_passwordMsg", "#new_password_div", "#new_passwordTips", "");
        return true;
    } else {
        fieldValidate("#new_passwordMsg", "#new_password_div", "#new_passwordTips", "密码在6-16个字符内!");
        return false;
    }
    return true;
}
//验证新密码格式 end

//验证新密码格式 start
function checkConfirmPassword() {
    var confirm_Password = $("#confirm_Password");
    var confirm_PasswordMsg = $("#confirm_PasswordMsg");
    var confirmStr = confirm_Password.val();
    if (confirmStr == "") {
        fieldValidate("#confirm_PasswordMsg", "#confirm_Password_div", "#confirm_PasswordTips", "确认新密码不能为空!");
        return false;
    }
    var confirm_PasswordImg = $("#confirm_PasswordImg");
    if (confirmStr.length >= 6 && confirmStr.length <= 16) {
        var new_password = $("#new_password").val();
        if (new_password == confirmStr) {
            fieldValidatePass("#confirm_PasswordMsg", "#confirm_Password_div", "#confirm_PasswordTips", "");
            return true;
        } else {
            fieldValidate("#confirm_PasswordMsg", "#confirm_Password_div", "#confirm_PasswordTips", "确认密码与新密码不一致!");
            if (new_password.length >= 6 && new_password.length <= 16) {
                var new_passwordMsg = $("#new_passwordMsg");
                new_passwordMsg.html("");
                fieldValidatePass("#new_passwordMsg", "#new_password_div", "#new_passwordTips", "");
            }
            return false;
        }
    } else {
        fieldValidate("#confirm_PasswordMsg", "#confirm_Password_div", "#confirm_PasswordTips", "密码在6-16个字符内!");
        return false;
    }
    return true;
}
//验证新密码格式 end

//弹出消息框 start
function showDialog(msg) {
    alert(msg);
}
//弹出消息框 end
// 验证不通过显示错误提示 start
function fieldValidate(msgId, divId, tipId, str) {
    $(msgId).html(str);
    $(tipId).fadeIn(500);
    $(divId).find(".pass").hide();
    $(divId).find(".add-r").hide();
}
// 验证不通过显示错误提示 end

// 验证通过显示正确提示 start
function fieldValidatePass(msgId, divId, tipId, str) {
    $(tipId).hide();
    $(divId).find(".pass").fadeIn(500);
}
// 验证通过显示正确提示 end